import base64
import requests

def get_token():
    # client_id 为官网获取的AK， client_secret 为官网获取的SK
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】'
    response = requests.get(host)
    if response:
        return response.json()


def baidu_imageclsfy(image):
    """ 百度ai图像识别 - 图像单主体识别 """
    request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/object_detect?access_token=" + access_token
    f = open('[本地文件]', 'rb')
    img = base64.b64encode(f.read())
    params = {
        "image":img
    }
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response= requests.post(request_url, data=params, headers=headers)
    return response.json()  
    
def baidu_imageclsfy(image):
	""" 百度ai图像识别 - 手部关键点识别 """
	request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/hand_analysis" + access_token
	f = open('[本地文件]', 'rb')
	img = base64.b64encode(f.read())
	params = {
		"image":img
	}
	headers = {
		'content-type': 'application/x-www-form-urlencoded'
	}
	response= requests.post(request_url, data=params, headers=headers)
	
	return response.json()

